<?php
namespace app\controller\admin\system\auth;

use app\BaseController;
use app\common\repositories\system\SystemMenuRepository;
use app\validate\admin\MenuValidate;
use think\App;

class Menu extends BaseController
{
    protected SystemMenuRepository $repository;
    protected string $menuType = 'admin';

    public function __construct(App $app, SystemMenuRepository $repository)
    {
        parent::__construct($app);
        $this->repository = $repository;
    }

    public function getList()
    {
        $data = $this->repository->getList([],$this->menuType);
        $data['list'] = formatCategory($data['list'], 'menu_id');
        return app('json')->success($data);
    }

    public function create(MenuValidate $validate)
    {
        $data = $this->checkParam($validate);
        if (!$data['pid']) $data['pid'] = 0;
        if ($data['pid'] && !$this->repository->mExists($data['pid'], $this->menuType))
            return app('json')->fail('父级分类不存在');
        $data['type'] = $this->menuType;
        $this->repository->create($data);
        return app('json')->success('添加成功');
    }

    public function update($id, MenuValidate $validate)
    {
        $data = $this->checkParam($validate);
        if (!$data['pid']) $data['pid'] = 0;
        if (!$this->repository->mExists($id, $this->menuType))
            return app('json')->fail('数据不存在');
        if ($data['pid'] && !$this->repository->mExists($data['pid'], $this->menuType))
            return app('json')->fail('父级分类不存在');
        $this->repository->update($id, $data);
        return app('json')->success('编辑成功');
    }


    public function createForm()
    {
        return app('json')->success(formToData($this->repository->menuForm($this->menuType)));
    }

    public function updateForm($id)
    {
        if (!$this->repository->mExists($id, $this->menuType))
            return app('json')->fail('数据不存在');
        return app('json')->success(formToData($this->repository->updateMenuForm($id, $this->menuType)));
    }

    private function checkParam(MenuValidate $validate)
    {
        $data = $this->request->params([['pid', 0], 'icon', 'menu_name', 'route', ['params', '[]'], 'sort', ['is_show', 0], ['is_menu', 0]]);
        if ($data['is_menu'] != 1) $validate->isAuth();
        $validate->check($data);
        return $data;
    }


    public function delete($id)
    {
        if ($this->repository->pidExists($id))
            return app('json')->fail('存在下级,无法删除');
        $this->repository->delete($id);
        return app('json')->success('删除成功');
    }

    public function menus()
    {
        $pre = '/admin'.config('admin.admin_prefix');
        $menus = $this->request->adminInfo()->level
            ? $this->repository->ruleByMenuList($this->request->adminRule(),$this->menuType)
            : $this->repository->getValidMenuList($this->menuType);
        foreach ($menus as $k => $menu) {
            $menu['route'] = $pre . $menu['route'];
            $menus[$k] = $menu;
        }
        return app('json')->success(array_values(array_filter(formatCategory($menus, 'menu_id'), function ($v) {
            return 0 == $v['pid'];
        })));
    }
}
